// pages/pay/pay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[
      // {
      //     id:1,
      //     goodsImage:"/images/lists01.webp",
      //     goodsName:"1商品名称",
      //     goodsAddress:"广州",
      //     goodsPrice:"200",
      //     num:1
      // },
      // {
      //     id:2,
      //     goodsImage:"/images/lists01.webp",
      //     goodsName:"2商品名称",
      //     goodsAddress:"广州",
      //     goodsPrice:"200",
      //     num:1
      // },
  ],
  lists:[],
  totalData:0,
  isshow:false,
  address:null,
  ischoose:false
  },
  chooseaddress(){
    this.setData({
      lists:wx.getStorageSync('addresslist'),
      ischoose:true
    })
    console.log(this.data.lists);
  },
  selectchoose(e){
    let index = e.currentTarget.dataset.choose;
    console.log(index);
    this.setData({
      address:this.data.lists[index],
      ischoose:false
    })
    console.log(this.data.address);
  },
  getaddress(){
    let select = wx.getStorageSync('selectaddress')
    if(select){
      this.setData({
        address:select,
        isshow:true
      })
    }else{
      let address = wx.getStorageSync('addresslist')
      if(address){
        this.setData({
          address:address[0],
          isshow:true
        })
      }else{
        this.setData({
          isshow:false
        })
      }
      
    }
  },
  getselectcoupon(){
    let selectcoupon = wx.getStorageSync('selectcoupon')
    if(selectcoupon){
      let off = selectcoupon.price;
      this.setData({
        totalData:this.data.totalData-off
      })
      console.log(this.data.totalData);
    }else{
      return;
    }
  },
  gocoupon(){
    let couponlist = wx.getStorageSync('coupon');
    if(couponlist){
      wx.navigateTo({
        url: '/pages/coupon/coupon',
      })
    }else{
      wx.showToast({
        title: '你还没有优惠券哦',
        icon: 'none',
        duration:3000
       })
    }
    
  },
  goOrder(){
    if(this.data.address){
      wx.navigateTo({
        url: '/pages/order/order',
      })
      this.setOrderLists(2);
    }else{
      wx.showToast({
        title: '地址不能为空',
        icon: 'none',
        duration: 2000
       })
    }
    
  },
  setOrderLists(type){
    let orderData={
      orderId:new Date().getTime(),
      orderLists:this.data.goodsList,
      type,//1:待支付2:待发货3:待收货4:待评价
      total:this.data.totalData
    }
    // orderData.orderLists = this.data.goodsList;
    let orderLists = wx.getStorageSync('orderLists');
    if(orderLists){
      orderLists.push(orderData);
      wx.setStorageSync('orderLists', orderLists)
    }else{
      wx.setStorageSync('orderLists', [orderData])
    }
  },
  // 计算总价格
  getTotal:function(){
    // 获取数据
    let goodsList = this.data.goodsList;

    let data = goodsList.reduce(function(total,item){
        return total + item.num *item.goodsPrice;
    },0)
    
    this.setData({
        totalData:data
    })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      goodsList:wx.getStorageSync('goodslist')
  })
    this.getTotal();
    this.getaddress();
    this.getselectcoupon();
    this.setOrderLists(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if(wx.getStorageSync('selectcoupon')){
      let coulist = wx.getStorageSync('coupon');
      coulist.forEach((item,index)=>{
        if(item.id==wx.getStorageSync('selectcoupon').id){
          coulist.splice(index,1);
        }
      })
      wx.setStorageSync('coupon', coulist);
      wx.removeStorageSync('selectcoupon');
    }
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('goodslist')
    this.setOrderLists(1);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})